//
//  MTNewPaymentItemCell.swift
//  MT
//
//  Created by ooops on 2020/5/14.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTNewPaymentItemCell: UICollectionViewCell {
    class func reuseIdentity() -> String {
        return "MTNewPaymentItemCellidentity"
    }
    
    var model: MTPaymentResultModel? {
        didSet {
            if model != nil {
                if let innerPrice = model?.money {
                    self.titleLabel.attributedText = self.mtArrtibute(text: "\(innerPrice)", icon: "blue_dimond", position: 1)
                }
                
                if let price = model?.rmbAmount {
                    //self.rmbLabel.text = "需要支付 " + String.init(format: "%@", "\(price)")
                    self.subTitleLabel.text = "\(price)元"
                }
                if model?.isSelected == true {
                    //self.gLayer.removeFromSuperlayer()
                    contentView.layer.borderColor = UIColor(hex: 0xffc000).cgColor
                    contentView.layer.borderWidth = 3
                    moneySelectedView.isHidden = false
                } else {
                    contentView.layer.borderColor = UIColor.text.x9b9b9b.cgColor
                    contentView.layer.borderWidth = 1
                    moneySelectedView.isHidden = true
                }
            }
        }
    }
    
    lazy var titleLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(20)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "6"
        return l
    }()
    
    lazy var iconView: UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "blue_dimond"))
        imageV.sizeToFit()
        return imageV
    }()
    
    lazy var subTitleLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "6"
        return l
    }()
    
    lazy var moneySelectedView: UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "money-selected"))
        imageV.sizeToFit()
        imageV.isHidden = true
        return imageV
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
        contentView.layer.borderColor = UIColor.text.x9b9b9b.cgColor
        contentView.layer.borderWidth = 1
        contentView.layer.cornerRadius = 4
    }
    
    private func mtArrtibute(text: String, icon: String, position: Int, iconSize: CGSize? = CGSize(width: 24, height: 24), iconYFix: CGFloat = -6, textColor: UIColor = UIColor.text.x1d1a1a) -> NSAttributedString {
        
        let markAttribute = NSMutableAttributedString(string: text)
        
        let arrtibuteValue: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : textColor,
                                                             NSAttributedString.Key.font : PingFangSCSemiboldFont(20)]
        
        markAttribute.addAttributes(arrtibuteValue, range: NSRange(location: 0, length: text.count))
        
        let markattch = NSTextAttachment()
        
        markattch.image = UIImage(named: icon)
        
        markattch.bounds = CGRect(x: 0, y: iconYFix, width: iconSize!.width, height: iconSize!.height)
        
        let markattchStr = NSAttributedString(attachment: markattch)
        
        /// icon 插入的位置
        if position == 0 {
            markAttribute.insert(markattchStr, at: 0)
        } else {
            markAttribute.append(markattchStr)
        }
        
        return markAttribute
    }
    
    private func setupUI() {
        contentView.addSubview(titleLabel)
        //contentView.addSubview(iconView)
        contentView.addSubview(subTitleLabel)
        contentView.addSubview(moneySelectedView)
    }
    
    private func makeConstraint() {
        titleLabel.snp.makeConstraints { (make) in
            //make.right.equalTo(contentView.snp.centerX)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(contentView.snp.centerY).offset(-1)
        }
        
//        iconView.snp.makeConstraints { (make) in
//            make.left.equalTo(contentView.snp.centerX)
//            make.centerY.equalTo(titleLabel)
//        }
        
        subTitleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(contentView.snp.centerY).offset(1)
        }
        
        moneySelectedView.snp.makeConstraints { (make) in
            make.right.bottom.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}
